<!DOCTYPE html>
<html lang=zh>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>Laya使用zip压缩包 | Vitiu</title>
  <meta name="description" content="JSZip游戏中有大量配置文件时，经常为了减少配置文件的加载时间需要将配置文件打包成zip文件，然后只需加载一次zip文件，使用JSZip库将配置文件从zip文件中解压读取出来即可。JSZip官网：JSZip官方提供的JSZip中只有JS文件，我们使用TS开发时为了方便使用需要d.ts文件。这里我提供一个我自己使用dtsmake转换后的3.1版本的JSzip库和jszip.d.ts文件的下载地址。">
<meta name="keywords" content="Laya,JSZip,插件">
<meta property="og:type" content="article">
<meta property="og:title" content="Laya使用zip压缩包">
<meta property="og:url" content="http:&#x2F;&#x2F;yoursite.com&#x2F;2019&#x2F;11&#x2F;23&#x2F;Laya%E4%BD%BF%E7%94%A8zip%E5%8E%8B%E7%BC%A9%E5%8C%85&#x2F;index.html">
<meta property="og:site_name" content="Vitiu">
<meta property="og:description" content="JSZip游戏中有大量配置文件时，经常为了减少配置文件的加载时间需要将配置文件打包成zip文件，然后只需加载一次zip文件，使用JSZip库将配置文件从zip文件中解压读取出来即可。JSZip官网：JSZip官方提供的JSZip中只有JS文件，我们使用TS开发时为了方便使用需要d.ts文件。这里我提供一个我自己使用dtsmake转换后的3.1版本的JSzip库和jszip.d.ts文件的下载地址。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https:&#x2F;&#x2F;upload-images.jianshu.io&#x2F;upload_images&#x2F;18005075-73003dbf5ed0a3c8.png?imageMogr2&#x2F;auto-orient&#x2F;strip%7CimageView2&#x2F;2&#x2F;w&#x2F;1240">
<meta property="og:image" content="https:&#x2F;&#x2F;upload-images.jianshu.io&#x2F;upload_images&#x2F;18005075-0028086de243eaf6.png?imageMogr2&#x2F;auto-orient&#x2F;strip%7CimageView2&#x2F;2&#x2F;w&#x2F;1240">
<meta property="og:updated_time" content="2019-11-23T14:11:01.543Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https:&#x2F;&#x2F;upload-images.jianshu.io&#x2F;upload_images&#x2F;18005075-73003dbf5ed0a3c8.png?imageMogr2&#x2F;auto-orient&#x2F;strip%7CimageView2&#x2F;2&#x2F;w&#x2F;1240">
  <!-- Canonical links -->
  <link rel="canonical" href="http://yoursite.com/2019/11/23/Laya%E4%BD%BF%E7%94%A8zip%E5%8E%8B%E7%BC%A9%E5%8C%85/index.html">
  
    <link rel="alternate" href="/atom.xml" title="Vitiu" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/css/style.css">
  
  
  
  
</head>


<body class="main-center theme-black" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="" target="_blank">
          <img class="img-circle img-rotate" src="/images/avatar.png" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">V</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Game Developer &amp; Designer</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> AnHui, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Laya-FairyGui/">Laya & FairyGui</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Laya%E6%8F%92%E4%BB%B6/">Laya插件</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/FairyGui/" rel="tag">FairyGui</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JSZip/" rel="tag">JSZip</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Laya/" rel="tag">Laya</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%8F%92%E4%BB%B6/" rel="tag">插件</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">十一月 2019</a><span class="archive-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/Laya-FairyGui/">Laya & FairyGui</a>
              </p>
              <p class="item-title">
                <a href="/2019/11/23/GObject/" class="title">Laya FairyGui系列二  GObject  GComponent</a>
              </p>
              <p class="item-date">
                <time datetime="2019-11-23T12:25:02.000Z" itemprop="datePublished">2019-11-23</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/Laya-FairyGui/">Laya & FairyGui</a>
              </p>
              <p class="item-title">
                <a href="/2019/11/23/FairyGui%E6%8E%A5%E5%85%A5/" class="title">Laya FairyGui系列一  FairyGui接入</a>
              </p>
              <p class="item-date">
                <time datetime="2019-11-23T12:22:03.000Z" itemprop="datePublished">2019-11-23</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/Laya%E6%8F%92%E4%BB%B6/">Laya插件</a>
              </p>
              <p class="item-title">
                <a href="/2019/11/23/Laya%E4%BD%BF%E7%94%A8zip%E5%8E%8B%E7%BC%A9%E5%8C%85/" class="title">Laya使用zip压缩包</a>
              </p>
              <p class="item-date">
                <time datetime="2019-11-23T11:51:55.000Z" itemprop="datePublished">2019-11-23</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-Laya使用zip压缩包" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      Laya使用zip压缩包
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/2019/11/23/Laya%E4%BD%BF%E7%94%A8zip%E5%8E%8B%E7%BC%A9%E5%8C%85/" class="article-date">
	  <time datetime="2019-11-23T11:51:55.000Z" itemprop="datePublished">2019-11-23</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/categories/Laya%E6%8F%92%E4%BB%B6/">Laya插件</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/tags/JSZip/" rel="tag">JSZip</a>, <a class="article-tag-link" href="/tags/Laya/" rel="tag">Laya</a>, <a class="article-tag-link" href="/tags/%E6%8F%92%E4%BB%B6/" rel="tag">插件</a>
  </span>


        

        <!-- <span class="post-comment"><i class="icon icon-comment"></i> <a href="/2019/11/23/Laya%E4%BD%BF%E7%94%A8zip%E5%8E%8B%E7%BC%A9%E5%8C%85/#comments" class="article-comment-link">评论</a></span> -->
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <h1 id="JSZip"><a href="#JSZip" class="headerlink" title="JSZip"></a>JSZip</h1><p>游戏中有大量配置文件时，经常为了减少配置文件的加载时间需要将配置文件打包成zip文件，然后只需加载一次zip文件，使用JSZip库将配置文件从zip文件中解压读取出来即可。<br>JSZip官网：<a href="https://stuk.github.io/jszip/" target="_blank" rel="noopener">JSZip</a><br>官方提供的JSZip中只有JS文件，我们使用TS开发时为了方便使用需要d.ts文件。这里我提供一个我自己使用<a href="https://github.com/ConquestArrow/dtsmake" target="_blank" rel="noopener">dtsmake</a><br>转换后的3.1版本的JSzip库和jszip.d.ts文件的<a href="[https://download.csdn.net/download/u010020342/11990490](https://download.csdn.net/download/u010020342/11990490)">下载地址</a>。</p>
<h1 id="引用JSZip"><a href="#引用JSZip" class="headerlink" title="引用JSZip"></a>引用JSZip</h1><ul>
<li><p>将js文件复制到项目的bin/libs文件夹下。<br><img src="https://upload-images.jianshu.io/upload_images/18005075-73003dbf5ed0a3c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="JSZip_0.png"></p>
</li>
<li><p>将d.ts文件复制到项目的libs文件夹下。<br><img src="https://upload-images.jianshu.io/upload_images/18005075-0028086de243eaf6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="JSZip_1.png"></p>
</li>
<li><p>在index.js文件中引入js库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">loadLib(&apos;libs/jszip.js&apos;)</span><br></pre></td></tr></table></figure>
<h1 id="读取zip包中文本内容"><a href="#读取zip包中文本内容" class="headerlink" title="读取zip包中文本内容"></a>读取zip包中文本内容</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">//先以二进制方式加载zip包</span><br><span class="line">Laya.loader.load([&#123; type: Laya.Loader.BUFFER, url: &apos;res/testFileZip.zip&apos; &#125;], Laya.Handler.create(this, () =&gt; &#123;</span><br><span class="line">	const jsZip = new JSZip();</span><br><span class="line">	//获取ZIP包内容传入JSZip中解析</span><br><span class="line">	jsZip.loadAsync(Laya.loader.getRes(&apos;res/testFileZip.zip&apos;)).then((data: any) =&gt; &#123;</span><br><span class="line">		//以text的方式读取testFile.txt内容</span><br><span class="line">		data.file(&apos;testFile.txt&apos;).async(&apos;text&apos;).then((content:string) =&gt;&#123;</span><br><span class="line">			console.log(content);</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;))</span><br></pre></td></tr></table></figure>
<h1 id="读取zip包中图片的base64信息"><a href="#读取zip包中图片的base64信息" class="headerlink" title="读取zip包中图片的base64信息"></a>读取zip包中图片的base64信息</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">//先以二进制方式加载zip包</span><br><span class="line">Laya.loader.load([&#123; type: Laya.Loader.BUFFER, url: &apos;res/testFileZip.zip&apos; &#125;], Laya.Handler.create(this, () =&gt; &#123;</span><br><span class="line">	const jsZip = new JSZip();</span><br><span class="line">	//获取ZIP包内容传入JSZip中解析</span><br><span class="line">	jsZip.loadAsync(Laya.loader.getRes(&apos;res/testFileZip.zip&apos;)).then((data: any) =&gt; &#123;</span><br><span class="line">		//以base64的方式读取skillIcon.png并显示在屏幕上</span><br><span class="line">		data.file(&apos;skillIcon.png&apos;).async(&apos;base64&apos;).then((content:string) =&gt;&#123;</span><br><span class="line">			const testPng = new Laya.Image();</span><br><span class="line">			testPng.skin = &apos;data:image/png;base64,&apos; + content</span><br><span class="line">			//testPng.loadImage();</span><br><span class="line">			Laya.stage.addChild(testPng);</span><br><span class="line">			console.log(content);</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;))</span><br></pre></td></tr></table></figure>
</li>
</ul>

      
    </div>
    <div class="article-footer">
      




    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/2019/11/23/FairyGui%E6%8E%A5%E5%85%A5/" title="Laya FairyGui系列一  FairyGui接入"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    
  </ul>
  
  
  
  <div class="bar-right">
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
    <div class="copyright">
    	<!-- 
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div> -->
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/js/plugin.min.js"></script>
<script src="/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>





   




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: '',
    appKey: '',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: false
  });
  </script>

     







</body>
</html>